<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script>
</head>

<body>
    <button class="GETBtn">GET请求</button>
    <button class="POSTBtn">POST请求</button>
    <button class="JSONPBtn">JSONP请求</button>

    <script>
        $(function () {
            // 发送GET请求
            $('.GETBtn').click(() => {

                $.ajax({
                    method: 'GET',
                    url: 'http://127.0.0.1/api/get',
                    data: {
                        status: 200,
                        msg: "GET请求成功",
                        data: {
                            name: 'zs',
                            age: 30,
                            gender: '男'
                        }
                    }
                })
            })

            // 发送post请求
            $('.POSTBtn').click(() => {
                $.ajax({
                    method: 'POST',
                    url: 'http://127.0.0.1/api/post',
                    data: {
                        status: 200,
                        msg: 'POST请求成功',
                        data: {
                            books: [
                                {
                                    bookname: '水浒传',
                                    author: '施耐庵'
                                },
                                {
                                    bookname: '红楼梦',
                                    author: '曹雪芹'
                                }
                            ]
                        }
                    }
                })
            })
            // 使用Ajax发送jsonp请求
            $('.JSONPBtn').on("click", function () {

                $.ajax({
                    method: 'GET',
                    url: "http://127.0.0.1/api/jsonp",
                    dataType: 'jsonp',
                    jsonp: 'callback',
                    success: function (res) {
                        console.log(res);
                    }
                })
            })

        })


        function callback(data) {
            console.log('jsonp接口回调函数' + data);
        }


    </script>
</body>

</html>